package ${controllerPkg};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.saoft.fangcms.common.enums.StatusEnum;
import com.saoft.fangcms.common.utils.ResultVoUtil;
import com.saoft.fangcms.common.utils.StatusUtil;
import com.saoft.fangcms.common.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};

import java.util.List;

/**
 * @author 小懒虫
 * @date 2019/4/4
 */
@Controller
@RequestMapping("${requestMapping}")
public class ${table.controllerName} {

    @Autowired
    private ${table.serviceName} ${name}Service;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("${permissions}:index")
    public String index(Model model, ${entity} ${name}){

        // 创建匹配器，进行动态查询匹配

        // 获取数据列表
        Page<${entity}> list = ${name}Service.page(new Page<>());

        // 封装数据
        model.addAttribute("list", list.getRecords());
        model.addAttribute("page", list);
        return "${requestMapping}/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("${permissions}:add")
    public String toAdd(){
        return "${requestMapping}/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("${permissions}:edit")
    public String toEdit(@PathVariable("id") Integer id, Model model){
        ${entity} ${name} = ${name}Service.getById(id);
        model.addAttribute("${name}", ${name});
        return "${requestMapping}/add";
    }

    /**
     * 保存添加/修改的数据
     */
    @PostMapping("/save")
    @RequiresPermissions({"${permissions}:add", "${permissions}:edit"})
    @ResponseBody
    public ResultVo save(${entity} ${name}){
        // 保存数据
        if(${name}.getId()!=null){
            ${name}Service.updateById(${name});
        }else{
            ${name}Service.save(${name});
        }

        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("${permissions}:detail")
    public String toDetail(@PathVariable("id") ${entity} ${name}, Model model){
        model.addAttribute("${name}",${name});
        return "${requestMapping}/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("${permissions}:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids){
        //todo 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        //if (${name}Service.updateStatus(statusEnum, ids)) {
        if (true) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}
